<template>
  <div class="tablePanel">
    <div class="tableHeader" v-if="isShowHead">
      <div class="page-title">{{tableTitle}}</div>
      <div class="btn-box">
        <slot></slot>
        <layout-dropdown :dropdown-list="tableSelfList" @dropdownChange="dropdownChange" :isOperating="isOperating"
                         dropdownTitle="调整列表布局" v-if="isShowDrop && tableSelfList.length > 4"></layout-dropdown>
      </div>
    </div>
    <div class="tableArea">
      <el-table :data="tableData" :ref="tableName" v-loading="tableLoading" :emptyText="emptyText"
                @selection-change="handleSelectionChange" @select-all="selectAll" @select="select">
        <el-table-column type="selection" align="center" width="55" v-if="isSelection"
                         :selectable="selectable"></el-table-column>
        <el-table-column fixed="left" align="center" width="72" v-if="customizeCheckboxProp">
          <template slot="header" slot-scope="scope">
            <el-checkbox v-model="customizeCheckboxProp.checkboxAll"
                         :disabled="customizeCheckboxProp.checkboxAllDisabled"
                         @change="customizeCheckboxChange({selectType: 'all', value: customizeCheckboxProp.checkboxAll})"></el-checkbox>
          </template>
          <template slot-scope="scope">
            <div class="customizeCheckbox">
              <el-checkbox v-model="scope.row.isSelect" :disabled="scope.row.disabled"
                           @change="customizeCheckboxChange(scope.row)"></el-checkbox>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="left" align="center" type="index" label="序号" width="72"
                         v-if="isShowIndex"></el-table-column>
        <el-table-column :fixed="item.fixed" :align="item.btns&&item.btns.length>1?'align-left':'center'"
                         :label="item.title" :width="item.limitWidth" :min-width="item.width"
                         v-for="(item,index) in tableSelfList"
                         :key="index" v-if="item.isSelected" :show-overflow-tooltip="item.showTooltip"
                         :class-name="item.name === 'handler'? 'handler': ''">
          <template slot-scope="scope">
            <div class="rate-box" v-if="item.type==='rate'">
              <el-rate v-model="scope.row[item.name]" :colors="colors" disabled></el-rate>
              <div class="left20">{{scope.row[item.name]}}</div>
            </div>
            <el-color-picker v-model="scope.row[item.name]" v-else-if="item.type==='color'" disabled></el-color-picker>
            <template v-else>
              <div v-if="scope.row[item.name] || parseInt(scope.row[item.name]) == 0" v-html="scope.row[item.name]"
                   :class="{overflow: item.showTooltip}" :style="{color: scope.row[item.name+'_color']}"></div>
              <div v-else :class="item.btns&&item.btns.length>1?'align-left':''" class="align-center">
                <zy-button :class="btn.type" v-if="scope.row[btn.type]" v-for="btn in item.btns"
                           :type="btn.btnType || 'primary'" mini text-button
                           @click="tableClick(scope.row, btn.type)" :key="btn.type">
                  {{btn.name || scope.row[btn.code]}}
                </zy-button>
              </div>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pageArea" v-if="isShowPagination">
      <el-pagination background layout="total, sizes,prev, pager, next,jumper" @size-change="sizeChange"
                     @current-change="pageChange" :page-sizes="[10, 20, 30, 50]" :total="total"
                     :current-page="currentPage" :page-size="pageSize"></el-pagination>
    </div>
  </div>
</template>

<script>
	export default {
		name: "tablePanel",
		props: {
			//是否需要表格标题
			isShowHead: {
				type: Boolean,
				default: () => true
			},
			//是否需要翻页栏
			isShowPagination: {
				type: Boolean,
				default: () => true
			},
			//是否需要调整布局下拉
			isShowDrop: {
				type: Boolean,
				default: () => true
			},
			//表格标题
			tableTitle: {
				type: String,
				default: ''
			},
			// 表格面板名称（务必保证此名称唯一，此名称用于缓存表格面板的展示数据）
			tableName: {
				type: String,
				default: () => ''
			},
			// 分页参数：数据总数
			total: {
				type: Number,
				default: () => 0
			},
			// 分页参数：一页展示数
			pageSize: {
				type: Number,
				default: () => 10
			},
			// 分页参数：当前页码
			currentPage: {
				type: Number,
				default: () => 1
			},
			// 表格参数：表格数据
			tableData: {
				type: Array,
				default: () => []
			},
			/**
			 * 表格参数：表格展示数据类型  （todo 暂时列出目前所需要的参数，若有新增待后续补充   2021/01/21 zf）
			 * 示例参数 {
			 *      title： ''               表头名称
			 *      name： ''                对应表格展示数据名称，如 tableData 中有 code: 1 则name: 'code'，若表格对应展示为按钮则需给出对应名称，如当前表格展示为查看按钮则name: 'lookBtn'
			 *      width: width             表格单元格宽度
			 *      isShow:true              是否在更多列表展示
			 *      btns: []                 slot中的按钮  与isSlot绑定，目前系统中表格slot大部分为按钮形式展示
			 *      isSelected： true        是否选中参数，用于调整列表布局
			 * }
			 */
			tableList: {
				type: Array,
				default: () => []
			},
			// 表格参数：表格loading
			tableLoading: {
				type: Boolean,
				default: () => false
			},
			// 表格参数：是否需要多选按钮
			isSelection: {
				type: Boolean,
				default: () => false
			},
			// 表格参数：是否需要序号
			isShowIndex: {
				type: Boolean,
				default: () => true
			},
			emptyText: {
				type: String,
				default: () => "暂无数据"
			},
			// 表格参数：是否是自定义多选
			customizeCheckboxProp: {
				type: Object,
				default: () => {
				}
			},
		},
		data() {
			return {
				colors: ['#FFAE02', '#FFAE02', '#FFAE02'],
				isOperating: false,
				tableSelfList: [],
				checkAll: false,
			};
		},
		computed: {},
		watch: {
			tableData(val) {
				this.$nextTick(() => {
					this.$refs[this.tableName].doLayout();
				});
			},
			tableList: {
				handler(newVal) {
					this.tableSelfList = JSON.parse(JSON.stringify(newVal)).filter(item => {
						item.isSelected = typeof (item.isSelected) === 'boolean' ? item.isSelected : true;
						return item;
					});

					this.tableSelfList.forEach(item => {
						if (item.title == '操作') {
							this.isOperating = true;
						}
					});

					localStorage.setItem(this.tableName, JSON.stringify(this.tableSelfList));
				},
				deep: true
			},
			customizeCheckboxProp: {
				handler(newVal) {
					this.checkAll = newVal.checkboxAll;
				},
				deep: true
			},
		},
		components: {},
		methods: {
			customizeCheckboxChange(val) {
				this.$emit('customizeCheckboxChange', val)
			},
			sizeChange(val) {
				this.$emit('sizeChange', val);
			},
			pageChange(val) {
				this.$emit('pageChange', val);
			},
			dropdownChange(hideList) {
				let tableList = this.tableSelfList.filter(item => {
					item.isSelected = hideList.indexOf(item.name) === -1;
					return item;
				});
				localStorage.setItem(this.tableName, JSON.stringify(tableList))
			},
			tableClick(info, type) {
				this.$emit('tableClick', info, type)
			},
			handleSelectionChange(val) {
				this.$emit('selectionChange', val)
			},
			selectAll(val) {
				this.$emit('selectAll', val)
			},
			select(val) {
				this.$emit('select', val)
			},
			selectable(row) {
				return row.isSelect;
			},
		},
		mounted() {
			// let tableList = JSON.parse(localStorage.getItem(this.tableName)) || [];
			// if (tableList && tableList.length) {
			// 	this.tableSelfList = JSON.parse(JSON.stringify(tableList));
			// } else {
				this.tableSelfList = JSON.parse(JSON.stringify(this.tableList)).filter(item => {
					item.isSelected = typeof (item.isSelected) === 'boolean' ? item.isSelected : true;
					return item;
				});
			// }

			this.tableSelfList.forEach(item => {
				if (item.title == '操作') {
					this.isOperating = true;
				}
			});

			localStorage.setItem(this.tableName, JSON.stringify(this.tableSelfList));
		},
		created() {
		}
	}
</script>

<style scoped lang="less">
  @import "../../style/common/base.less";

  @thBgColor: #F0F0F0;
  @boxShadowColor: rgba(0, 0, 0, 0.1);

  .tablePanel {
    border-radius: @commonBorderRadius;
    width: 100%;
    background: @allWhite;
    box-shadow: 0px 0px 4px 0px @boxShadowColor;

    .tableHeader {
      padding: 0 @commonPaddingOutside;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .page-title {
        position: relative;
        font-size: 16px;
        font-weight: bold;
        margin: 16px 0;
        color: @fontColor;

        &:after {
          content: '';
          position: absolute;
          top: 0;
          bottom: 0;
          left: -@commonPaddingOutside;
          width: 4px;
          background: @allWarringColor;
        }
      }

      .btn-box {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        flex: 1;
        padding: @commonPaddingOutside 0;

        button {
          margin: 0 0 0 12px;
          text-align: right;
        }
      }

      .layoutDropdown {
        margin-left: 12px;
      }
    }

    .tableArea {

      .align-center {
        text-align: center;

        &.align-left {
          text-align: left;
        }

        .zy-btn {
          margin-right: 8px;
        }
      }

      .rate-box {
        display: flex;
        align-items: center;

        .left20 {
          margin-left: 20px;
        }
      }

      .overflow {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        height: 40px;
        line-height: 40px;
      }

      /deep/ .el-table {
        tr {
          height: 46px;
          line-height: 46px;
        }

        td, th {
          padding: 0;
        }

      }

      /deep/ .el-table__empty-text {
        min-height: 46px;
      }

      /deep/ .el-table .cell.el-tooltip > div {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
    }

    .pageArea {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 10px 0;
    }

    /deep/ .el-table th {
      height: 48px;
      font-size: 14px;
      font-weight: bold;
      color: @fontColor;
      background: @thBgColor;
    }

    /deep/ .el-table tr {
      height: 46px;
      color: @fontColor;
    }

    /deep/ .el-table__body tr.hover-row.current-row > td,
    /deep/ .el-table__body tr.hover-row.el-table__row--striped.current-row > td,
    /deep/ .el-table__body tr.hover-row.el-table__row--striped > td,
    /deep/ .el-table__body tr.hover-row > td {
      background-color: rgba(0, 119, 251, 0.2);
    }

    /deep/ .el-table tr:nth-child(odd) {
      background: @allWhite;
    }

    /deep/ .el-table tr:nth-child(even) {
      background: #f0f0f0;
    }

    /deep/ .el-table td, .el-table th.is-leaf {
      border-color: @borderColor;
    }

    /deep/ .el-table td.is-align-left {
      box-shadow: -1px 0px 1px 0px rgba(0, 0, 0, 0.1);
      background-color: @allWhite !important;
    }

    /deep/ .el-table td.is-align-left {
      box-shadow: -1px 0px 1px 0px rgba(0, 0, 0, 0.1);
      background-color: @allWhite !important;
    }

    /deep/ .el-table td.handler {
      box-shadow: -1px 0px 1px 0px rgba(0, 0, 0, 0.1);
      background-color: @allWhite !important;
    }


    /*分页*/

    /deep/ .el-pagination {
      color: @fontColor;
    }

    /deep/ .el-pagination .el-pagination__total {
      font-size: 12px;
      color: @fontColor;
    }

    /deep/ .el-pagination .el-pagination__sizes {
      font-size: 12px;
      color: @fontColor;
    }

    /deep/ .el-pagination .el-input__inner {
      border: 1px solid #C7CBD1;
      color: @fontColor;
      border-radius: 4px;
      font-size: 12px;
    }

    /deep/ .el-pagination .el-pager li {
      font-size: 12px;
      font-weight: normal;
    }

    /deep/ .el-pagination .el-pager li.active {
      background-color: @allPrimaryColor;
    }

    /deep/ .el-pagination .el-pager li:not(.disabled).active {
      background-color: @allPrimaryColor;
    }

  }
</style>
